package com.redhat.auth.web.account;

import java.util.Date;
import java.util.Set;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionSupport;
import com.redhat.auth.application.AccountApplication;
import com.redhat.auth.application.BizSystemApplication;
import com.redhat.auth.domain.Account;
import com.redhat.auth.domain.BizSystem;
import com.redhat.auth.domain.BizSystemBinding;
import com.redhat.auth.domain.Role;
import com.redhat.redwork.coc.RedworkAction;

/**
 *  Account帐号的辅助Action,用于弹出窗口的跳转及Request数据的存储
 * @author lingen.liu
 *
 */
public class ParamAccountAction extends ActionSupport implements RedworkAction{

	
	private String password;
	
	private String passwordConfirm;
	
	private String id;
	
	private String systemIds;
	
	@Autowired
	private AccountApplication accountApplication;
	
	/**
	 * 密码重置时，跳转到pop-account-man-reset.ftl页面，并存储ID及帐号名称
	 * @return
	 */
	public String getPasswordReset(){
		ServletActionContext.getRequest().setAttribute("id", id);
		return "pop-account-man-reset";
	}
	
	
	/**
	 * 重置密码
	 * @return
	 */
	public String passwordReset(){
		Account account = accountApplication.getAccount(Long.parseLong(id));
		account.setPassword(password);
		account.setPasswordLastUpdateDate(new Date());
		accountApplication.update(account);
		return "json";
		
	}
	
	
	/**
	 * 修改一个帐号和系统绑定的状态
	 */
	public String resetLock(){
		String status = ServletActionContext.getRequest().getParameter("status");
		if(status.equals("1")){
			accountApplication.setBizSystemBindingStatus(Long.parseLong(id),Long.parseLong(systemIds), BizSystemBinding.Status.ENABLED);
		}else{
			accountApplication.setBizSystemBindingStatus(Long.parseLong(id),Long.parseLong(systemIds), BizSystemBinding.Status.DISABLED);
		}
		return "json";
	}
	
	/**
	 * 将一个帐号同多个系统进行绑定
	 * @return
	 */
	public String accountBindSystems(){
		accountApplication.accountBindSystems(Long.parseLong(id), systemIds.split(","));
		return "json";
	}
	
	
	/**
	 * 将一个帐号同多个系统解除绑定
	 * @return
	 */
	public String accounUnBindSystems(){
		accountApplication.accountUnBindSystem(Long.parseLong(id), systemIds.split(","));
		return "json";
	}
	
	/**
	 * 将一个系统同多个帐号绑定
	 */
	public String systemBindAccounts(){
		accountApplication.systemBindAccount(Long.parseLong(systemIds), id.split(","));
		return "json";
	}
	
	/**
	 * 将一个系统同多个帐号解除绑定
	 */
	public String systemUnBindAccounts(){
		accountApplication.systemUnBindAccount(Long.parseLong(systemIds), id.split(","));
		return "json";
	}
	
	
	
	/**
	 * 解除锁定
	 * @return
	 */
	public String unlockAccount(){
		String selectedIds = ServletActionContext.getRequest().getParameter("selectedIds");
		String ids[] = selectedIds.split(",");
		Account account = null;
		for(String id:ids){
		    account = accountApplication.getAccount(Long.parseLong(id));
		    accountApplication.unLock(account);
		}
		return "json";
	}
	
	/**
	 * 锁定帐号
	 * @return
	 */
	public String lockAccount(){
		String selectedIds = ServletActionContext.getRequest().getParameter("selectedIds");
		String ids[] = selectedIds.split(",");
		Account account = null;
		for(String id:ids){
		    account = accountApplication.getAccount(Long.parseLong(id));
		    accountApplication.lock(account);
		}
		return "json";
	}
	
	
	/**////////////角色分配action add by jiangw 2010.04.26///////////////*/
	/**
	 * 账户角色分配预处理（页面跳转）
	 */
	public String preAddRoles(){
		ServletActionContext.getRequest().setAttribute("id", id);
		return "pop-addRoles";
	}
	
	/**
	 * 账户角色分配
	 */
	public String addRoles(){
		String selectedIds = ServletActionContext.getRequest().getParameter("selectedIds");
		String roleIds[] = selectedIds.split(",");
		Account account = accountApplication.getAccount(Long.parseLong(id));
		
		Set<Role> bindRoles = account.getRoles();
		bindRoles.clear();
		for(String roleId:roleIds){
			Role role = new Role();
			role.setId(Long.parseLong(roleId));
			bindRoles.add(role);
		}
		account.setRoles(bindRoles);
		accountApplication.update(account);
		return SUCCESS;
	}
	

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordConfirm() {
		return passwordConfirm;
	}

	public void setPasswordConfirm(String passwordConfirm) {
		this.passwordConfirm = passwordConfirm;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public AccountApplication getAccountApplication() {
		return accountApplication;
	}

	public void setAccountApplication(AccountApplication accountApplication) {
		this.accountApplication = accountApplication;
	}

	public String getSystemIds() {
		return systemIds;
	}

	public void setSystemIds(String systemIds) {
		this.systemIds = systemIds;
	}
	
	
}
